home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!news
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c++
- Subject: Re: Help! What's wrong with this?
- Date: Fri, 19 Apr 1996 14:32:07 GMT
- Organization: Netcom
- Message-ID: <3177a34c.144208280@nntp.ix.netcom.com>
- References: <4l22v3$1beo@useneta1.news.prodigy.com> <317660A6.56C7@datalytics.com>
- NNTP-Posting-Host: ix-dc14-19.ix.netcom.com
- X-NETCOM-Date: Fri Apr 19 9:31:49 AM CDT 1996
- X-Newsreader: Forte Agent .99d/32.182
-
- Rob Stewart <stew@datalytics.com> wrote:
-
- > Brian Munroe wrote:
- > >
- > > I'm trying to learn C++ and I've run into a strange problem in the
- > > following code.
- > >
- >
- > First, the code you submitted is C, not C++. Just because
- > you're using a C++ compiler on it does not make it C++. Let's
- > call it what it is.
-
- OK. Let's call it C++. The program Brian submitted will not compile
- on any properly working C compiler.
-
- >
- > > int const ADD_LEN = 40;
- >
- > Try "const int ADD_LEN = 40" instead.
-
- Just because Brian is obviously a beginner, there's no reason to give
- him poor advice. A programmer should not "try" things -- he should
- understand the language definition. One who does will know that "int
- const ADD_LEN = 40;" and "const int ADD_LEN = 40;" are equivalent.
- There's no reason to change it.
-
- >
- > > int main()
- > > {
- > > struct cust_list {
- > > char name[ADD_LEN];
- > > char add1[ADD_LEN];
- > > char add2[ADD_LEN];
- > > char add3[ADD_LEN];
- > > } customer;
- > > int flag;
- > >
- > > void get_customer_info(char temp[ADD_LEN], int flag);
- >
- > This declaration should really come before main.
- >
- > >
- > > for (flag=0; flag<4; flag++)
- > > { switch (flag)
- > > { case 0: get_customer_info(customer.name, flag); break;
- > > case 1: get_customer_info(customer.add1, flag); break;
- > > case 2: get_customer_info(customer.add2, flag); break;
- > > case 3: get_customer_info(customer.add3, flag); break;
- > > default: exit(1);
- > > }
- > > }
- >
- > This is the oddest thing I've ever seen. Don't play games like
- > this, just write the following:
- >
- > get_customer_info(customer.name, 0);
- > get_customer_info(customer.add1, 1);
- > get_customer_info(customer.add2, 2);
- > get_customer_info(customer.add3, 3);
- >
- > Having said that, it would be better for you to supply the
- > prompt string to get_customer_info rather than using a flag to
- > do it. This allows the caller to more appopriately associate
- > the text of the prompt with the buffer being filled.
- >
- > > return 0;
- > > }
- > >
- > > void get_customer_info(char temp[ADD_LEN], int flag)
- >
- > FYI: Declaring temp as char[ADD_LEN] doesn't guarrantee that you
- > get an array of that size.
- >
- > > { int answer;
- > > int len;
- > > clrscr();
- > > switch (flag)
- > > { case 0:
- > > cout << "Enter the bidder's name in one of the following forms.\n";
- > > cout << " Person: LAST NAME, FIRST NAME\n";
- > > cout << " Business: COMPANY NAME\n\n";
- > > break;
- > > case 1:
- > > cout << "\nEnter first line of address\n";
- > > break;
- > > case 2:
- > > cout << "\nEnter second line of address\n";
- > > break;
- > > case 3:
- > > cout << "\nEnter third line of address\n";
- > > break;
- > > default:
- > > exit(1);
- > > }
- > > cin >> temp;
- >
- > Try this instead. It will eat whitespace before filling temp.
- >
- > cin >> ws >> temp;
- >
-
- As opposed to
-
- cin >> temp;
-
- which eats whitespace before filling temp. The code Brian gave is
- correct -- why suggest a change that does nothing.
-
- > > len = strlen(temp);
- > > if (len < ADD_LEN)
- > > { strncat(temp, " ",ADD_LEN-
- > > len);}
- > > temp[ADD_LEN-1] = '\0';
- >
- > Rather than have get_customer_info be dependent upon a global
- > value, why not pass the size of temp to it. This reduces
- > dependence on the global to main, which is a better habit to
- > develop (later maintenance of code is easier with restricted
- > dependence on globals).
- >
- > > return;
- > > }
- > >
- > > When I run this all 4 prompts are outputted (Is that a word?), but only
- > > input for the name and second line are accepted. The input line doesn't
- > > accept input for the first and third lines. I'm guessing that there is a
- > > return character caught in the input buffer after I input the name and
- > > second address lines and it's read in for the first and third lines. If
- > > so, how do I get it out of the buffer and what the heck is it doing there
- > > in the first place !?
-
- Brian's problem is that he is entering a name that contains spaces.
- The the first input gets the string from the first non-whitespace
- character to the first space and the next input gets the next string
- from what's already been entered.
-
-
- Michael M Rubenstein
-